package com.hson.demo.base.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

@SuppressWarnings({"unchecked","rawtypes"})
public class CommonUtils {

	public static List changeList(List list, String key) {

		List li = new ArrayList();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		if (list != null && list.size() > 0) {
			// Calendar c = Calendar.getInstance();
			for (int i = 0; i < list.size(); i++) {

				Map<String, Object> map = (Map<String, Object>) list.get(i);
				Date str = (Date) map.get(key);
				String date = format.format(str);
				li.add(date);
			}
		}

		return li;
	}

	public static List findUnRegistration(String date, List list) {

		List li = new ArrayList();

		if (StringUtils.isNotBlank(date) && date.length() > 5) {
			String year = date.substring(0, 4);
			String month = date.substring(5, 7);

			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, Integer.parseInt(year));
			calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);

			String time = year + month;

			Date today = new Date();
			String nowdate = DateUtil.format(today, "yyyyMMdd");
			String nowtime = nowdate.substring(0, 6);

			int max = 0;
			if (time.equals(nowtime)) {
				max = Integer.parseInt(nowdate.substring(6, 8));
			} else if (time.compareTo(nowtime) < 0) {
				max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
			}

			for (int i = 1; i <= max; i++) {
				String dates = time + (i > 9 ? i : "0" + i);

				Map<String, Object> map = new HashMap<String, Object>();
				if (list == null || !list.contains(dates)) {
					Date d = DateUtil.parseToDate(dates, "yyyyMMdd");

					map.put("time", d.getTime());
					map.put("address", "");
					map.put("lng", "");
					map.put("lat", "");

					li.add(map);
				}
			}
		}

		return li;
	}

	public static String changeToString(List list) {

		String str = "";
		if (list != null && list.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < list.size(); i++) {
				String st = (String) list.get(i);
				if (StringUtils.isNotBlank(st)) {
					sb.append(st).append(",");
				}

			}

			str = sb.substring(0, sb.lastIndexOf(",")).toString();
		}
		return str;
	}

	/**
	 * 返回６位随机数字
	 * 
	 * @return
	 */
	public static int getSixInt() {
		Random r = new Random();
		int x = r.nextInt(999999);

		if (x > 100000) {
			return x;
		}
		return 123456;
	}

}
